package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.CertificateReqDTO;
import com.iafme.dto.res.CertificateResDTO;
import com.iafme.model.Certificate;
import com.iafme.service.CertificateService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/3/24 21:23
 */
@RestController
@RequestMapping(value = "/certificate")
@AllArgsConstructor
public class CertificateController implements BaseCommonInterface<CertificateReqDTO, CertificateResDTO> {


    private final CertificateService certificateService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "证书信息")
    @SaCheckPermission("enter:certificate:add")
    @Override
    public Result<String> save(@Valid CertificateReqDTO certificateReqDTO) {
        Certificate certificate = Convert.convert(Certificate.class, certificateReqDTO);
        certificateService.save(certificate);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "证书信息")
    @SaCheckPermission("enter:certificate:edit")
    @Override
    public Result<String> update(@Valid CertificateReqDTO certificateReqDTO) {
        Certificate certificate = Convert.convert(Certificate.class, certificateReqDTO);
        certificateService.updateById(certificate);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "证书信息")
    @SaCheckPermission("enter:certificate:delete")
    @Override
    public Result<String> del(Integer id) {
        return null;
    }

    @Override
    public Result<Page<CertificateResDTO>> lists(CertificateReqDTO certificateReqDTO) {
        return Result.successResult(certificateService.lists(certificateReqDTO));
    }

    @Override
    public Result<CertificateResDTO> findById(Integer id) {
        return null;
    }

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "证书信息")
    @SaCheckPermission("enter:certificate:delete")
    @Override
    public Result<String> batchDel(List<CertificateResDTO> list) {
        List<Integer> ids = list.stream().map(CertificateResDTO::getId).collect(Collectors.toList());
        certificateService.removeByIds(ids);
        return Result.successResult();
    }
}
